package com.keqiang.chart.utils

import android.graphics.Color
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import me.zhouzhuo810.magpiex.utils.SimpleUtil

// <editor-fold desc="Number扩展">

internal var isInEditMode2 = false
internal inline val Float.px: Float get() = if (isInEditMode2) this else SimpleUtil.getScaledValue(this)

/**
 * 角度转化为弧度
 */
inline val Float.toRadian: Float get() = (this / 180f * Math.PI).toFloat()

/**
 * 弧度转化为角度
 */
inline val Float.toDegrees: Float get() = (this / Math.PI * 180f).toFloat()

/**
 * 将角度转化为0°~360°范围内
 */
inline val Float.to360Range: Float
    get() {
        var angle2 = this
        while (angle2 < 0f) angle2 += 360f
        return angle2 % 360f
    }

// </editor-fold>

/**
 * 改变颜色亮度
 *
 * @param luminance 亮度值，数值越大，颜色越亮
 */
fun setColorLuminance(@ColorInt color: Int, @FloatRange(from = 0.0) luminance: Float): Int {
    val tempFloatArray = ObjectPool.obtainFloatArray(3)
    Color.RGBToHSV(Color.red(color), Color.green(color),
        Color.blue(color), tempFloatArray)
    tempFloatArray[2] *= if (luminance < 0f) 0f else luminance
    val newColor = Color.HSVToColor(Color.alpha(color), tempFloatArray)
    ObjectPool.recycleFloatArray(tempFloatArray)
    return newColor
}

/**
 * 获取颜色亮度，0~1，数值越大，颜色越亮
 */
fun getColorLuminance(@ColorInt color: Int): Float {
    val tempFloatArray = ObjectPool.obtainFloatArray(3)
    Color.RGBToHSV(Color.red(color), Color.green(color),
        Color.blue(color), tempFloatArray)
    val luminance = tempFloatArray[2]
    ObjectPool.recycleFloatArray(tempFloatArray)
    return luminance
}

